/*
 * @Author: wuhao930406 1148547900@qq.com
 * @Date: 2023-07-20 15:12:07
 * @LastEditors: wuhao930406 1148547900@qq.com
 * @LastEditTime: 2023-08-25 15:32:23
 * @FilePath: /legion-iot/src/pages/dashboard3d/components/Building.jsx
 * @Description:
 *
 * Copyright (c) 2023 by ${git_name_email}, All Rights Reserved.
 */
import React, { useState, useEffect, useRef } from "react";
import { Canvas, useLoader, useThree } from "@react-three/fiber";
import {
  OrbitControls,
  Environment,
  useGLTF,
  Box,
  Gltf,
  RoundedBox,
  Text,
} from "@react-three/drei";
import Zuo from "./cast/zuo";
import Zhong from "./cast/zhong";
import You from "./cast/you";
import Model from "./cast/model";
import { outsidedata } from "../components/cast/config";
import BoxA from "../components/cast/BoxA";

const Floor = ({ color, position, scale }) => (
  <Box
    args={[3000, 0.2, 3000]}
    position={position}
    scale={scale}
    castShadow
    receiveShadow
  >
    <meshStandardMaterial color={color} />
  </Box>
);

const Building = ({ score, activeFloor, setIndex, theindex }) => {
  console.log(activeFloor);

  return (
    <Canvas
      style={{ height: "100%", width: "100%" }}
      camera={{
        position: [200, 150, 200],
      }}
    >
      <color attach="background" args={["#333"]} />
      <ambientLight intensity={0.5} />
      <pointLight position={[10, 10, 10]} />

      {score < 60 ? (
        <>
          <Model></Model>
          {/* <FloorWithRooms position={[0, 60, 0]}></FloorWithRooms> */}
          <Floor color="#666" position={[0, 0, 0]}></Floor>
        </>
      ) : (
        <>
          {outsidedata?.map((it, i) => {
            return (
              <BoxA
                {...it}
                index={i}
                key={i}
                setIndex={setIndex}
                theindex={theindex}
              ></BoxA>
            );
          })}

          {(activeFloor?.includes("3单元") || !activeFloor) && (
            <You
              activeFloor={activeFloor}
              position={[0, 1, 0]}
              setIndex={setIndex}
              theindex={theindex}
            ></You>
          )}

          {(activeFloor?.includes("2单元") || !activeFloor) && (
            <Zhong
              activeFloor={activeFloor}
              position={[0, 1, 0]}
              setIndex={setIndex}
              theindex={theindex}
            ></Zhong>
          )}

          {(activeFloor?.includes("1单元") || !activeFloor) && (
            <Zuo
              activeFloor={activeFloor}
              position={[0, 1, 0]}
              setIndex={setIndex}
              theindex={theindex}
            ></Zuo>
          )}

          <Gltf
            src="https://ng-website.oss-cn-hangzhou.aliyuncs.com/glb/dimian.glb"
            receiveShadow
            castShadow
            inject={
              activeFloor && (
                <meshPhysicalMaterial
                  color={"lightblue"}
                  transparent={true} //是否支持透明度
                  opacity={0.2} //透明度
                  //visible={true}//是否可见
                  //metalness={1} //金属性贴图
                  roughness={0} //粗糙程度 越精细镜面效果越强
                  clearcoat={1} // 透明漆 罩光漆
                  transmission={0} //透光效果 模型透明 表面效果处理不变 opactiy是整体透明 不削弱反射光
                  // reflectivity={1} //光的 反射率
                />
              )
            }
          ></Gltf>
          <Gltf
            src="https://ng-website.oss-cn-hangzhou.aliyuncs.com/glb/shu.glb"
            receiveShadow
            castShadow
            position={[0, 1, 0]}
            inject={
              activeFloor && (
                <meshPhysicalMaterial
                  color={"lightblue"}
                  transparent={true} //是否支持透明度
                  opacity={0.2} //透明度
                  //visible={true}//是否可见
                  //metalness={1} //金属性贴图
                  roughness={0} //粗糙程度 越精细镜面效果越强
                  clearcoat={1} // 透明漆 罩光漆
                  transmission={0} //透光效果 模型透明 表面效果处理不变 opactiy是整体透明 不削弱反射光
                  // reflectivity={1} //光的 反射率
                />
              )
            }
          />
          <OrbitControls></OrbitControls>
        </>
      )}
    </Canvas>
  );
};

export default Building;
